package com.linjuke.beginsum.common;

import java.net.InetAddress;
import java.net.NetworkInterface;

public class NetWorkUtil {

	public static String getMacAddress() {
		StringBuilder sb = new StringBuilder();
		try {
			String hostAddress = getHostAddress();
			NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getByName(hostAddress));
			byte[] macs = ni.getHardwareAddress();

			for (int i = 0; i < macs.length; i++) {
				String unitItem = Integer.toHexString(macs[i] & 0xFF);
				if (unitItem.length() == 1) {
					sb.append('0');
				}
				sb.append(unitItem);
				sb.append('-');
			}
			String mac = sb.toString();
			mac = mac.substring(0, mac.length() - 1);
			return mac;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String getHostAddress() {
		try {
			return InetAddress.getLocalHost().getHostAddress();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
